/**
 * Created by no1 on 2015/8/11.
 */

'use strict';
let DD = require('./daoLoader');
let F = require('../function')
let Base = require('./Base');
let util = require("util");
let appConfigDao = new DD.mongoDB.AppConfigDao();

function AppConfigDispatcher(data) {
  data = data || {};
  data.dao = data.dao || appConfigDao;
  data.esDao = data.esDao || {};
  data.redisDao = data.redisDao || {};
  data.modelName = data.modelName || 'appConfig';
  data.modelType = data.modelType || 'mongo';
  this.data = data;
  Base.call(this, this.data);
};
util.inherits(AppConfigDispatcher, Base);

function* judgeSort(obj) {
  let newObj = {};
  for (let sort in obj) {
    if (obj[sort] && obj[sort].length == 0) continue;
    let newSort = [];
    for (let one of obj[sort]) {
      if (!one.def) one.def = 0;
      newSort.push(one);
    }
    newObj[sort] = newSort;
  }
  return newObj;
}

function* judgeModel(model) {
  let newModel = [];
  for (let one of model) {
    if (!one.status) one.status = 1;
    newModel.push(one);
  }
  return newModel;
}


/**
 *@todo app配置的添加
 *@param  {Object}  fields  所有参数
 */
AppConfigDispatcher.prototype.add = function*(fields) {
  let parameters = {};
  parameters.id = 1;
  if (fields.H5_model.length == 0) return F.returnData(1, '必须有H5_model');
  //拼接检验参数o2o_warelist_sort,o2o_shoplist_sort
  //let obj = {o2o_warelist_sort: fields.o2o_warelist_sort, o2o_shoplist_sort: fields.o2o_shoplist_sort};
  //let retSorls = yield judgeSort(obj);
  //for (let i in retSorls) parameters[i] = retSorls[i];

  //parameters.H5_model_regular = fields.H5_model_regular || '';
  //parameters.H5_model_URL = fields.H5_model_URL || '';
  //parameters.H5_zipurl = fields.H5_zipurl || '';
  //parameters.H5_model = fields.H5_model || '';
  parameters.H5_model = yield judgeModel(fields.H5_model);

  //搜索保存
  let ret = yield this.dao.findAndSave({id: parameters.id}, parameters);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.date);
  return F.returnData(0, ret.msg, ret.data);
};

/**
 *@todo 更新
 *@param  {Object}  fields  所有参数
 */
AppConfigDispatcher.prototype.update = function*(fields) {
  let updateBody = fields.update;
  let parameters = {};
  if (updateBody.H5_model.length == 0) return F.returnData(1, '必须有H5_model');

  //拼接检验参数o2o_warelist_sort,o2o_shoplist_sort
  //let obj = {o2o_warelist_sort: updateBody.o2o_warelist_sort, o2o_shoplist_sort: updateBody.o2o_shoplist_sort};
  //let retSorls = yield judgeSort(obj);
  //for (let i in retSorls) parameters[i] = retSorls[i];
  //检验H5_model
  if (updateBody.H5_model && updateBody.H5_model.length != 0) parameters.H5_model = yield judgeModel(updateBody.H5_model);
  //if (updateBody.H5_model_regular) parameters.H5_model_regular = updateBody.H5_model_regular;
  //if (updateBody.H5_model_URL) parameters.H5_model_URL = updateBody.H5_model_URL;
  //if (updateBody.H5_zipurl) parameters.H5_zipurl = updateBody.H5_zipurl;

  //更新保存
  let ret = yield this.dao.findAndUpdate({id: 1}, parameters);
  if (ret.error == 1) return F.returnData(1, ret.msg);

  return F.returnData(0, ret.msg, ret.data);

};

/**
 *@todo 获取
 *@param  {Object}  fields  所有参数
 */
AppConfigDispatcher.prototype.detail = function*(fields) {
  let ret = yield this.dao.getOneByQuery({id: 1});
  if (ret.error == 1) return F.returnData(1, ret.msg);

  //过滤数据
  let json = JSON.parse(JSON.stringify(ret.data));//去掉继承来的东西
  let retData = F.dataFilter(json, ['_id', '__v', 'id', 'createdAt', 'updateAt'])
  return F.returnData(0, ret.msg, retData);
};

module.exports = AppConfigDispatcher;